<? if ($c = stack('name')) {

	$_id_component	= $DATA['components'][$c]['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value

	$_spam_check	= "/http:\/\/|href=|url=/i";

	$_crit = "id_data=$_id_data AND id_parent=$_id_component";
	
	$_bits = array('required' => 0x01, 'multiple' => 0x02, 'listbox' => 0x04);
	
	$s = component_settings($c);


	$DATA['components'][$c]['data'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb('data'), 'where' => "$_crit",	'order' => 'id_index', 'index' => 'id' )));

	foreach($DATA['components'][$c]['data'] as $k => $v) {
		$v['kind'] = $v['tag'] & 0xff;
		
		$temp = $v['tag'] >> 8;
		foreach($_bits as $name => $mask)
			$v[$name] = $temp & $mask;

//		$v['required'] = ($v['tag'] >> 8) & 1;
	
		$v = array_merge($v, datafetch_array('meta', $_id_meta, $k, context('lang')));
		
		switch($v['kind']) {
			case 3: // select
				if ($p = $v['params']) 	// option field
					$v['options'] = explode("\n", $p);
				break;
			case 4:
				if ($p = $v['value']) 	// option field
					$v['options'] = array_trim_empty_str(explode(",", $p));
		}
			
		$DATA['components'][$c]['data'][$k] = $v;
	}


	if ((int) $_POST['check'] == $s['check']) { 
	
		try {

			foreach($DATA['components'][$c]['data'] as $k => $v)
				if (preg_match($_spam_check, postdata("field$k"))) {
					throw new Exception('Error: No URLs allowed');
//					exit( php2js(array("diags" => implode('', $GLOBALS['DIAGS']))) );
				}
	
			$req = array();
			foreach($DATA['components'][$c]['data'] as $k => $v) {
				if ($v['required'] && !postdata("field$k")) {
					$req[] = $v['title'];
				}
			}
			
			if (sizeof($req)) {
				throw new Exception($s['required'].' <strong>'.implode(', ', $req).'.</strong>');
//				diags('<p class="error">'.$s['required'].' <strong>'.implode(', ', $req).'.</strong></p>');
//				exit( php2js(array("diags" => implode('', $GLOBALS['DIAGS']))) );
			}
		
			$res = array("You've got a new message!\r\n");
			$files = array();
			
			$rt_name = $rt_mail = '';
		
			foreach($DATA['components'][$c]['data'] as $k => $v) {
				$v['title'] = str_replace('<strong>*</strong>', '', $v['title']);
				
				switch($v['kind']) {
					case 1: // text field
						$res[] = "\r\n$v[title]: ".$_POST["field$k"]."\r\n";
						
						if ($k == $s['rt_name']) $rt_name = trim($_POST["field$k"]);
						if ($k == $s['rt_mail']) $rt_mail = trim($_POST["field$k"]);
						break;
						
					case 2: // multiline text field
						$res[] = "\r\n$v[title]:\r\n".$_POST["field$k"]."\r\n";
						break;
						
					case 3:	// option list
		
						$t = "\r\n$v[title]:";
		
						if ($v['multiple']) {
							foreach($_POST["field$k"] as $a)
								$t .= "\r\n".$v['options'][$a];
						} else {
							$t .= ' '.$v['options'][$_POST["field$k"]];
						}
								
						$t .= "\r\n";
		
						$res[] = $t;
		
						break;
						
					case 4:
						if ($f = $_FILES["field$k"]) {
							$ext = parse_fname($f['name']);
							if (!(sizeof($v['options']) && (!$ext || in_array(strtolower($ext['ext']), $v['options'])))) {

								throw new Exception('Error: File types allowed are: <strong>'.implode(', ', $v['options']).'.</strong>');
								
//								diags('<p class="error">Error: File types allowed are: <strong>'.implode(', ', $v['options']).'.</strong></p>');
//								return 1;
							}
							
							$files[] = array('name' => $f['name'], 'data' => chunk_split(base64_encode(file_get_contents($f['tmp_name']))));
						}
						break;
				}
		
			}
		
	
	
	
			$subject = '=?utf-8?B?'.base64_encode( stripslashes("Feedback from the ".sta('project', 'company_short')." site")).'?=';
			
			$random_hash = md5(date('r', time())); 
			
			$headers = "From: $s[from]";
	
			if ($rt_mail)	$headers .= "\r\nReply-To: \"=?utf-8?B?".base64_encode( stripslashes($rt_name))."?=\" <$rt_mail>";
			
			$headers .= "\r\nX-Mailer: PHP/".phpversion();
			$headers .= "\r\nMIME-Version: 1.0";
			$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"mixed-".$random_hash."\"";
			$headers .= "\r\nContent-Transfer-Encoding: base64";
	
			$body = chunk_split(base64_encode(stripslashes(implode("", $res))));
	
	
					ob_start();
?>
--mixed-<?=$random_hash?>

Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

<?=$body?>

<? if (sizeof($files)) foreach($files as $f) { ?>
--mixed-<?=$random_hash?>

Content-Type: application/octet-stream; name="<?=$f['name']?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?=$f['name']?>"

<?=$f['data']?>

<? } ?>

--mixed-<?=$random_hash?>--

<?
	
			$body = ob_get_clean();
			
		if (@mail($s['contact'], $subject, $body, $headers, '-f '.stripslashes($s['from'])))
				diags('<p class="success">'.$s['sent'].'</p>');
			else
				throw new Exception($s['not_sent']);
				
//			exit( str_replace('<', '&lt;', json_encode(array("diags" => implode('', $GLOBALS['DIAGS'])))) );
		
		} catch(Exception $e) {
			diags('<p class="error">'.$e->getMessage().'</p>');
		}

		// ajax request - return json and exit			
		exit( str_replace('<', '&lt;', json_encode(array("diags" => implode('', $GLOBALS['DIAGS'])))) );
		
		
	}


	unstack();
}
